﻿using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Kelper.Domain.Models
{
    public class Recipe
    {
        [Key]
        public int ID { get; set; }

        [Display(Name = "Title")]
        [Required(ErrorMessage = "Please specify a title")]
        [MaxLength(40, ErrorMessage = "The title may not be longer than 40 characters")]
        public string Name { get; set; }

        [Display(Name = "Instructions")]
        [DataType(DataType.MultilineText)]
        [Required(ErrorMessage = "Please specify instructions")]
        [MinLength(20, ErrorMessage = "Please write some more text")]
        [MaxLength(4000, ErrorMessage = "The instructions may not be more than 4000 characters long")]
        public string Text { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime Created { get; set; }

        public virtual User Author { get; set; }

        public virtual Category Category { get; set; }

        public virtual ICollection<Ingredient> Ingredients { get; set; }
        public virtual ICollection<Tag> Tags { get; set; }

        public string NiceName { get { return Name.Replace("<3", "♥"); } }

        public string IngredientsCSV
        {
            get
            {
                return Ingredients == null ? "" :
                    String.Join(", ", Ingredients.Select(i => i.Name));
            }
        }

        public Recipe()
        {
            Created = DateTime.UtcNow;
        }

        public string UrlName()
        {
            return Regex.Replace(Name.Replace("&", "and").ToLower(),
                @"[^\w ]", "").Replace(' ', '-');
        }
    }
}